﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ExcelDataReader.Silverlight.Data;
using System.Windows.Input;

namespace SilveR
{
    public partial class ExcelSheetSelectionWindow : ChildWindow
    {
        private List<IWorkSheet> workSheets;

        public ExcelSheetSelectionWindow(IWorkBook excelWorkBook)
        {
            InitializeComponent();

            workSheets = (List<IWorkSheet>)excelWorkBook.WorkSheets;

            List<string> sheetNames = new List<string>();
            foreach (IWorkSheet sheet in workSheets)
            {
                ListBoxItemEx lbi = new ListBoxItemEx();
                lbi.Content = sheet.Name;
                lbi.MouseLeftButtonDown += new MouseButtonEventHandler(ElementMouseDown);
                lstWorkSheets.Items.Add(lbi);
            }

            //lstWorkSheets.ItemsSource = sheetNames;
        }

        public IWorkSheet SelectedWorkSheet
        {
            get
            {
                return workSheets[lstWorkSheets.SelectedIndex];
            }
        }

        private void lstWorkSheets_SelectedValueChanged(object sender, EventArgs e)
        {
            OKButton.IsEnabled = (lstWorkSheets.SelectedItem != null);
        }

        private void lstWorkSheets_DoubleClick(object sender, EventArgs e)
        {
            if (lstWorkSheets.SelectedItem != null)
            {
                this.DialogResult = true;
                this.Close();
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        //double click stuff...
        private bool clickedOnce = false;

        private void timer_Tick(object sender, EventArgs e)
        {
            clickedOnce = false;
        }

        private void ElementMouseDown(object sender, MouseEventArgs e)
        {
            if (clickedOnce) //then now on second click
            {
                this.DialogResult = true;
            }
            else clickedOnce = true;
        }
    }
}